package com.fan.share;

import android.view.View;
import android.widget.TextView;

import com.fan.share.adapter.CommonAdapter;
import com.fan.share.adapter.ViewHolder;
import com.fan.share.entity.Share;
import com.fan.share.entity.ShareSort;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.sharesdk.wechat.favorite.WechatFavorite;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 分享
 *
 * @author fan
 * @date 2018/12/6
 */
public class BottomSheetDialogShareFragment extends BaseBottomSheetDialogFragment {


    private List< ShareSort > mData = new ArrayList<>();

    private Share mShare;

    private static Share linkShare;

    public BottomSheetDialogShareFragment () {
    }

    private BottomSheetDialogShareFragment ( Share mShare ) {
        this.mShare = mShare;
    }

    public static void getInstance ( FragmentActivity activity, Share share, String str ) {
        BottomSheetDialogShareFragment fragment = new BottomSheetDialogShareFragment( share );
        fragment.show( activity.getSupportFragmentManager(), str );
    }

    public static void getInstance(FragmentActivity activity, Share share) {
        linkShare = share;
        BottomSheetDialogShareFragment fragment = new BottomSheetDialogShareFragment();
        fragment.show(activity.getSupportFragmentManager(), "shareLink");
    }

    @Override
    public int getScrollViewContentLayoutId () {
        return R.layout.fragment_bottom_sheet_dialog_share;
    }

    @Override
    protected void initView ( View view ) {
        TextView baseShareCancel = view.findViewById( R.id.base_share_cancel );
        mData.add( new ShareSort( "微信", R.mipmap.share_wechat, Wechat.NAME ) );
        mData.add( new ShareSort( "分享到朋友圈", R.mipmap.share_friend_circle, WechatMoments.NAME ) );
        mData.add( new ShareSort( "微信收藏", R.mipmap.share_collect, WechatFavorite.NAME ) );
        RecyclerView mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        CommonAdapter< ShareSort > mAdapter;
        mRecyclerView.setAdapter( mAdapter = new CommonAdapter< ShareSort >( getActivity(), mData, R.layout.item_share ) {
            @Override
            public void convert ( int position, ViewHolder helper, ShareSort item ) {
                helper.setImageResource( R.id.photo, item.getResId() );
                helper.setText( R.id.name, item.getName() );
            }
        } );
        mAdapter.setOnItemClickListener( ( view12, position ) -> {
            ShareSort shareSort = mData.get( position );
            if (mShare !=null){
                ShareFunction.showShare(shareSort.getPlatform(), mShare, getActivity());
            }else{
                ShareFunction.showShare(shareSort.getPlatform(), linkShare, getActivity());
            }
            dismiss();
        } );
        baseShareCancel.setOnClickListener( v -> dismiss() );

    }
}
